#!/bin/bash
# Copyright Broadcom, Inc. All Rights Reserved.
# SPDX-License-Identifier: APACHE-2.0
#
# Environment configuration for pgpool

# The values for all environment variables will be set in the below order of precedence
# 1. Custom environment variables defined below after Bitnami defaults
# 2. Constants defined in this file (environment variables with no default), i.e. BITNAMI_ROOT_DIR
# 3. Environment variables overridden via external files using *_FILE variables (see below)
# 4. Environment variables set externally (i.e. current Bash context/Dockerfile/userdata)

# Load logging library
# shellcheck disable=SC1090,SC1091
. /opt/bitnami/scripts/liblog.sh

export BITNAMI_ROOT_DIR="/opt/bitnami"
export BITNAMI_VOLUME_DIR="/bitnami"

# Logging configuration
export MODULE="${MODULE:-pgpool}"
export BITNAMI_DEBUG="${BITNAMI_DEBUG:-false}"

# By setting an environment variable matching *_FILE to a file path, the prefixed environment
# variable will be overridden with the value specified in that file
pgpool_env_vars=(
    PGPOOL_USER_CONF_FILE
    PGPOOL_USER_HBA_FILE
    PGPOOL_PASSWD_FILE
    PGPOOL_PORT_NUMBER
    PGPOOL_ENABLE_POOL_HBA
    PGPOOL_ENABLE_POOL_PASSWD
    PGPOOL_ENABLE_LOAD_BALANCING
    PGPOOL_ENABLE_STATEMENT_LOAD_BALANCING
    PGPOOL_DISABLE_LOAD_BALANCE_ON_WRITE
    PGPOOL_ENABLE_CONNECTION_CACHE
    PGPOOL_TIMEOUT
    PGPOOL_CONNECT_TIMEOUT
    PGPOOL_MAX_POOL
    PGPOOL_HEALTH_CHECK_PERIOD
    PGPOOL_HEALTH_CHECK_TIMEOUT
    PGPOOL_HEALTH_CHECK_MAX_RETRIES
    PGPOOL_HEALTH_CHECK_RETRY_DELAY
    PGPOOL_HEALTH_CHECK_PSQL_TIMEOUT
    PGPOOL_AUTO_FAILBACK
    PGPOOL_DISCARD_STATUS
    PGPOOL_SR_CHECK_USER
    PGPOOL_SR_CHECK_PASSWORD
    PGPOOL_SR_CHECK_DATABASE
    PGPOOL_SR_CHECK_PERIOD
    PGPOOL_HEALTH_CHECK_USER
    PGPOOL_HEALTH_CHECK_PASSWORD
    PGPOOL_ADMIN_USERNAME
    PGPOOL_ADMIN_PASSWORD
    PGPOOL_POSTGRES_USERNAME
    PGPOOL_POSTGRES_PASSWORD
    PGPOOL_POSTGRES_CUSTOM_USERS
    PGPOOL_POSTGRES_CUSTOM_PASSWORDS
    PGPOOL_ENABLE_LDAP
    PGPOOL_AUTHENTICATION_METHOD
    PGPOOL_AES_KEY
    PGPOOL_ENABLE_TLS
    PGPOOL_TLS_CA_FILE
    PGPOOL_TLS_CERT_FILE
    PGPOOL_TLS_KEY_FILE
    PGPOOL_TLS_PREFER_SERVER_CIPHERS
    PGPOOL_ENABLE_LOG_CONNECTIONS
    PGPOOL_ENABLE_LOG_HOSTNAME
    PGPOOL_ENABLE_LOG_PCP_PROCESSES
    PGPOOL_ENABLE_LOG_PER_NODE_STATEMENT
    PGPOOL_BACKEND_NODES
    PGPOOL_BACKEND_APPLICATION_NAMES
    PGPOOL_FAILOVER_ON_BACKEND_SHUTDOWN
    PGPOOL_FAILOVER_ON_BACKEND_ERROR
    PGPOOL_DAEMON_USER
    PGPOOL_DAEMON_GROUP
)
for env_var in "${pgpool_env_vars[@]}"; do
    file_env_var="${env_var}_FILE"
    if [[ -n "${!file_env_var:-}" ]]; then
        if [[ -r "${!file_env_var:-}" ]]; then
            export "${env_var}=$(< "${!file_env_var}")"
            unset "${file_env_var}"
        else
            warn "Skipping export of '${env_var}'. '${!file_env_var:-}' is not readable."
        fi
    fi
done
unset pgpool_env_vars

# Paths
export PGPOOL_BASE_DIR="${BITNAMI_ROOT_DIR}/pgpool"
export PGPOOL_BIN_DIR="${PGPOOL_BASE_DIR}/bin"
export PGPOOL_DATA_DIR="${PGPOOL_BASE_DIR}/data"
export PGPOOL_DEFAULT_CONF_DIR="${PGPOOL_BASE_DIR}/conf.default"
export PGPOOL_CONF_DIR="${PGPOOL_BASE_DIR}/conf"
export PGPOOL_DEFAULT_ETC_DIR="${PGPOOL_BASE_DIR}/etc.default"
export PGPOOL_ETC_DIR="${PGPOOL_BASE_DIR}/etc"
export PGPOOL_LOG_DIR="${PGPOOL_BASE_DIR}/logs"
export PGPOOL_TMP_DIR="${PGPOOL_BASE_DIR}/tmp"
export PGPOOL_INITSCRIPTS_DIR="/docker-entrypoint-initdb.d"
export PGPOOL_CONF_FILE="${PGPOOL_CONF_DIR}/pgpool.conf"
export PGPOOL_PCP_CONF_FILE="${PGPOOL_ETC_DIR}/pcp.conf"
export PGPOOL_PGHBA_FILE="${PGPOOL_CONF_DIR}/pool_hba.conf"
export PGPOOL_USER_CONF_FILE="${PGPOOL_USER_CONF_FILE:-}"
export PGPOOL_USER_HBA_FILE="${PGPOOL_USER_HBA_FILE:-}"
export PGPOOL_LOG_FILE="${PGPOOL_LOG_DIR}/pgpool.log"
export PGPOOL_PID_FILE="${PGPOOL_TMP_DIR}/pgpool.pid"
export PGPOOL_PASSWD_FILE="${PGPOOL_PASSWD_FILE:-pool_passwd}"
export PGPOOLKEYFILE="${PGPOOL_CONF_DIR}/.pgpoolkey"

# General Pgpool-II settings
export PGPOOL_PORT_NUMBER="${PGPOOL_PORT_NUMBER:-5432}"
export PGPOOL_ENABLE_POOL_HBA="${PGPOOL_ENABLE_POOL_HBA:-yes}"
export PGPOOL_ENABLE_POOL_PASSWD="${PGPOOL_ENABLE_POOL_PASSWD:-yes}"
export PGPOOL_ENABLE_LOAD_BALANCING="${PGPOOL_ENABLE_LOAD_BALANCING:-yes}"
export PGPOOL_ENABLE_STATEMENT_LOAD_BALANCING="${PGPOOL_ENABLE_STATEMENT_LOAD_BALANCING:-no}"
export PGPOOL_DISABLE_LOAD_BALANCE_ON_WRITE="${PGPOOL_DISABLE_LOAD_BALANCE_ON_WRITE:-transaction}"
export PGPOOL_ENABLE_CONNECTION_CACHE="${PGPOOL_ENABLE_CONNECTION_CACHE:-yes}"
export PGPOOL_TIMEOUT="${PGPOOL_TIMEOUT:-360}"
export PGPOOL_CONNECT_TIMEOUT="${PGPOOL_CONNECT_TIMEOUT:-10000}"
export PGPOOL_MAX_POOL="${PGPOOL_MAX_POOL:-15}"
export PGPOOL_HEALTH_CHECK_PERIOD="${PGPOOL_HEALTH_CHECK_PERIOD:-30}"
export PGPOOL_HEALTH_CHECK_TIMEOUT="${PGPOOL_HEALTH_CHECK_TIMEOUT:-10}"
export PGPOOL_HEALTH_CHECK_MAX_RETRIES="${PGPOOL_HEALTH_CHECK_MAX_RETRIES:-5}"
export PGPOOL_HEALTH_CHECK_RETRY_DELAY="${PGPOOL_HEALTH_CHECK_RETRY_DELAY:-5}"
export PGPOOL_HEALTH_CHECK_PSQL_TIMEOUT="${PGPOOL_HEALTH_CHECK_PSQL_TIMEOUT:-15}"
export PGPOOL_AUTO_FAILBACK="${PGPOOL_AUTO_FAILBACK:-no}"
export PGPOOL_DISCARD_STATUS="${PGPOOL_DISCARD_STATUS:-yes}"

# Authentication settings
export PGPOOL_SR_CHECK_USER="${PGPOOL_SR_CHECK_USER:-}"
export PGPOOL_SR_CHECK_PASSWORD="${PGPOOL_SR_CHECK_PASSWORD:-}"
export PGPOOL_SR_CHECK_DATABASE="${PGPOOL_SR_CHECK_DATABASE:-postgres}"
export PGPOOL_SR_CHECK_PERIOD="${PGPOOL_SR_CHECK_PERIOD:-30}"
export PGPOOL_HEALTH_CHECK_USER="${PGPOOL_HEALTH_CHECK_USER:-$PGPOOL_SR_CHECK_USER}"
export PGPOOL_HEALTH_CHECK_PASSWORD="${PGPOOL_HEALTH_CHECK_PASSWORD:-$PGPOOL_SR_CHECK_PASSWORD}"
export PGPOOL_ADMIN_USERNAME="${PGPOOL_ADMIN_USERNAME:-}"
export PGPOOL_ADMIN_PASSWORD="${PGPOOL_ADMIN_PASSWORD:-}"
export PGPOOL_POSTGRES_USERNAME="${PGPOOL_POSTGRES_USERNAME:-postgres}"
export PGPOOL_POSTGRES_PASSWORD="${PGPOOL_POSTGRES_PASSWORD:-}"
export PGPOOL_POSTGRES_CUSTOM_USERS="${PGPOOL_POSTGRES_CUSTOM_USERS:-}"
export PGPOOL_POSTGRES_CUSTOM_PASSWORDS="${PGPOOL_POSTGRES_CUSTOM_PASSWORDS:-}"
export PGPOOL_ENABLE_LDAP="${PGPOOL_ENABLE_LDAP:-no}"
export PGPOOL_AUTHENTICATION_METHOD="${PGPOOL_AUTHENTICATION_METHOD:-scram-sha-256}"
export PGPOOL_AES_KEY="${PGPOOL_AES_KEY:-head -c 20 /dev/urandom | base64}"

# TLS settings
export PGPOOL_ENABLE_TLS="${PGPOOL_ENABLE_TLS:-no}"
export PGPOOL_TLS_CA_FILE="${PGPOOL_TLS_CA_FILE:-}"
export PGPOOL_TLS_CERT_FILE="${PGPOOL_TLS_CERT_FILE:-}"
export PGPOOL_TLS_KEY_FILE="${PGPOOL_TLS_KEY_FILE:-}"
export PGPOOL_TLS_PREFER_SERVER_CIPHERS="${PGPOOL_TLS_PREFER_SERVER_CIPHERS:-yes}"

# Logging settings
export PGPOOL_ENABLE_LOG_CONNECTIONS="${PGPOOL_ENABLE_LOG_CONNECTIONS:-no}"
export PGPOOL_ENABLE_LOG_HOSTNAME="${PGPOOL_ENABLE_LOG_HOSTNAME:-no}"
export PGPOOL_ENABLE_LOG_PCP_PROCESSES="${PGPOOL_ENABLE_LOG_PCP_PROCESSES:-yes}"
export PGPOOL_ENABLE_LOG_PER_NODE_STATEMENT="${PGPOOL_ENABLE_LOG_PER_NODE_STATEMENT:-no}"

# PostgreSQL backend settings
export PGPOOL_BACKEND_NODES="${PGPOOL_BACKEND_NODES:-}"
export PGPOOL_BACKEND_APPLICATION_NAMES="${PGPOOL_BACKEND_APPLICATION_NAMES:-}"
export PGPOOL_FAILOVER_ON_BACKEND_SHUTDOWN="${PGPOOL_FAILOVER_ON_BACKEND_SHUTDOWN:-on}"
export PGPOOL_FAILOVER_ON_BACKEND_ERROR="${PGPOOL_FAILOVER_ON_BACKEND_ERROR:-off}"

# System settings
export PGPOOL_DAEMON_USER="${PGPOOL_DAEMON_USER:-pgpool}"
export PGPOOL_DAEMON_GROUP="${PGPOOL_DAEMON_GROUP:-pgpool}"

# Custom environment variables may be defined below
